<?php


namespace App\Web\Model;

use App\Web\Model\CaseResume as CaseResumeModel;
use EchoPHP\Facades\Db;
use EchoPHP\Facades\Validation;
use EchoPHP\Validator\Validator;
use Ramsey\Uuid\Uuid;

class Interview extends Model{

    /**
     * 添加面试通知
     * @param array $oprInfo
     * @param int $subId
     * @param $caseResumeIds
     * @param array $interviewParams
     * @param $sendDate
     * @param $interviewType
     * @param $templateType
     * @return array|bool
     * @throws \Exception
     */
    public function addInterviewNotice(array $oprInfo, int $subId, $projectId ,$caseResumeIds, array $interviewParams, $sendDate, $interviewType, $templateType) {
        if (!Validation::validateDateFormat('senddate', $sendDate, 'Y-m-d H:i:s')) {
            $sendDate = date('Y-m-d H:i:s');
        }
        //校验输入
        $interviewType = in_array($interviewType, [1, 2, 3]) ? $interviewType : 1;
        $templateType = in_array($templateType, [1, 2, 3]) ? $templateType : 1;

        $rule = [
            'jobname|面试岗位' => ['required', 'maxLength' => 20],
            'jobcompany|面试公司' => ['required', 'maxLength' => 30],
            'interviewtime|面试时间' => ['required', 'dateFormat' => 'Y-m-d H:i:s'],
            'address|面试地点' => ['required', 'maxLength' => 50],
            'contract|联系人' => ['required', 'maxLength' => 25],
            'mobilephone|联系人手机' => ['required', 'maxLength' => 20],
            'memo|注意事项' => ['maxLength' => 150],
        ];
        $msg = [
            'interviewtime' => '请填写正确的面试时间',
        ];
        $validate = new Validator($rule, $msg);
        if (!$validate->validate($interviewParams)) {
            return ['status' => false, 'message' => '字段校验失败', 'errorFields' => $validate->getErrors()];
        }

        $caseResumeModel = new CaseResumeModel();
        $searchParams = [
            'caseresumeid' => $caseResumeIds
        ];
        $resumeList = $caseResumeModel->getCaseResumeList($oprInfo['userid'], $subId, $projectId ,$searchParams);

        if (empty($resumeList)) {
            return ['status' => false, 'message' => '请选择简历',];
        }
        $batchCode = Uuid::uuid1()->toString();
        $nowDate = date('Y-m-d H:i:s');
        $resumeCount = count($resumeList);
        $interviewSql = 'INSERT INTO Interview(batchcode,creator,creatorname,createdate,updatedate,sendtype,templatetype,status,senddate,totalcount,sendbysys,jobname,jobcompany,interviewtime,address,contract,mobilephone,memo) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)';
        $interviewVar = [
            $batchCode, $oprInfo['userid'], $oprInfo['cname'], $nowDate, $nowDate, $interviewType, $templateType, '00', $sendDate, $resumeCount, 0, $interviewParams['jobname'], $interviewParams['jobcompany'], $interviewParams['interviewtime'], $interviewParams['address'], $interviewParams['contract'], $interviewParams['mobilephone'], $interviewParams['memo']
        ];
        $interviewDetailSql = 'INSERT INTO InterviewDetail(resumeid,subid,projectid,caseid,tagcode,creator,creatorname,createdate,updatedate,sendtype,status,senddate,batchcode,email,mobilephone,sendbysys,name) VALUES';
        $interviewDetailVar = [];
        foreach ($resumeList as $item) {
            $resumeEmail = $item['resumeinfo']['email'] ?? '';
            $resumePhone = $item['resumeinfo']['mobilephone'] ?? '';
            $name = $item['resumeinfo']['cname'] ?? '尊敬的先生/女士';
            $interviewDetailSql .= '(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?),';
            $interviewDetailVar = array_merge($interviewDetailVar, [
                $item['resumeid'], $item['subid'], $item['projectid'], $item['caseid'], $item['flowat'] . $item['flowatstatus'], $oprInfo['userid'], $oprInfo['cname'], $nowDate, $nowDate, $interviewType, '00', $sendDate, $batchCode, $resumeEmail, $resumePhone, 0, $name
            ]);
        }
        $interviewDetailSql = substr($interviewDetailSql, 0, -1);

        $interviewStatement = Db::prepareStatement($interviewSql, $interviewVar);
        $interviewDetailStatement = Db::prepareStatement($interviewDetailSql, $interviewDetailVar);

        Db::connect()->prepareTransactionStatement(false, $interviewStatement, $interviewDetailStatement)->execute();


        return ['status' => true, 'message' => '发送成功',];
    }
    /**
     * 查询总数
     * @param array $search
     * @param $field
     * @param int $rowFrom
     * @param int $rowTo
     * @return array
     */
    public function getCount($oprId,$search){
        $field = 'count(1) as count';
        return $this->selInterview($oprId,$search, $field)[0]['count'];
    }
    /**
     * 查询面试通知
     * @param array $search
     * @param $field
     * @param int $rowFrom
     * @param int $rowTo
     * @return array
     */
    public function selInterview($oprId,$search = [], $field,$rowFrom = 0, $rowTo = 0){
        if(empty($field)){
            $field  = 'id,batchcode,creator,createdate,updatedate,sendtype,templatetype,status,senddate,totalcount,sendbysys,jobname,jobcompany,interviewtime,address,contract,mobilephone,memo,creatorname';
        }
        $sql = 'SELECT ' . $field . ' FROM Interview';
        $var = [];
        $where = [];
        $where[] = 'creator = ?';
        $var[] = $oprId;
        //发送类型 1-短信 2-邮件 3-短信+邮件
        if (isset($search['sendtype']) && !empty($search['sendtype'])) {
           $where[] = 'sendtype = ?';
           $var[] = $search['sendtype'];
        }
        //模板类型 1-面试邀约 2-招聘会邀约 3-面试提醒
        if (isset($search['templatetype']) && !empty($search['templatetype'])) {
           $where[] = 'templatetype = ?';
           $var[] = $search['templatetype'];
        }
        $sql .= ' WHERE ' . implode(' AND ', $where);

        $sql .= ' ORDER BY id DESC';

        return Db::select($sql, $var, $rowFrom, $rowTo);
    }
}